package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ware.annotation.Token;
import com.example.ware.model.po.Fireworks;
import com.example.ware.model.po.Kinds;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.service.FireWorksService;
import com.example.ware.service.KindsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/kinds")
public class KindsController {
    @Autowired
    private KindsService kindsService;

    @Autowired
    private FireWorksService fireWorksService;

    //    获取种类
    @GetMapping("/list")
    public ResponseData getList() {
        return new ResponseData(kindsService.list(), true, "获取成功", 200);
    }



    @GetMapping("/page")
    public ResponseData getListPage(Page p) {
        Page<Kinds> kindList=kindsService.page(p);
        return new ResponseData(kindList, true, "获取成功", 200);
    }

    //    增加种类
    @Token
    @PostMapping("/add")
    public ResponseData getList(@RequestBody Kinds kinds) {
        kinds.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        boolean save = kindsService.save(kinds);
        if (save) {
            return new ResponseData(kindsService.list(), true, "添加成功", 200);
        } else {
            return new ResponseData(kindsService.list(), false, "添加失败", 201);
        }
    }


    //  删除
    @Token
    @DeleteMapping("/del")
    public ResponseData del(String id) {
        QueryWrapper<Fireworks> qw = new QueryWrapper<>();
        qw.eq("kid", id);
        List<Fireworks> list = fireWorksService.list(qw);
        if (list.size()!=0) {
            return new ResponseData(null, false, "该类别已经存在烟花数据", 201);
        } else {
            boolean b = kindsService.removeById(id);
            if (b) {
                return new ResponseData(null, true, "删除成功", 200);
            } else {
                return new ResponseData(null, false, "删除失败", 201);
            }
        }
    }


    //    修改
    @Token
    @PutMapping("/update")
    public ResponseData update(@RequestBody Kinds kinds) {
        boolean save = kindsService.updateById(kinds);
        if (save) {
            return new ResponseData(kindsService.list(), true, "修改成功", 200);
        } else {
            return new ResponseData(kindsService.list(), false, "修改失败", 201);
        }
    }
}
